package com.clh.seckill.validator;

import com.clh.seckill.utils.VaildatorUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author cailinhu
 * @Description 拓展自定义注解实现
 * @Date 17:28 2021-10-27
 * @Version 1.0
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {
    private boolean required=false;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
         required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (!required){
           //如果不是必填项，直接校验通过
          return true;
        }
        return VaildatorUtil.isMobile(s);
    }
}
